package aole.util;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class DialogCreator extends JDialog {
	private static final long	serialVersionUID	= 1L;
	
	private ArrayList< JComponent > comps;
	private int col1Width, col2Width;
	private int maxHeight;
	private JPanel pContents;
	
	public static final int OK_CANCEL_OPTION = 1;
	public static final int OK_OPTION = 2;
	public static final int CANCEL_OPTION = 3;
	
	private int selectedOption;
	
	public DialogCreator( Frame parent, String title ) {
		super( parent, title, true );
		
		comps = new ArrayList<>();
		
		setLayout( new BorderLayout() );
		pContents = new JPanel();
		pContents.setLayout( null );
		add( pContents, BorderLayout.CENTER );
		// buttons ok/cancel
		JButton btnOK = new JButton( "OK" );
		getRootPane().setDefaultButton( btnOK );
		btnOK.addActionListener( new ActionListener() {
			public void actionPerformed( ActionEvent e ) {
				selectOK();
			}
		});
		JButton btnCancel = new JButton( "Cancel" );
		btnCancel.addActionListener( new ActionListener() {
			public void actionPerformed( ActionEvent e ) {
				selectCancel();
			}
		});
		
		JPanel pButtons = new JPanel( new FlowLayout( FlowLayout.RIGHT ) );
		pButtons.add( btnCancel );
		pButtons.add( btnOK );
		add( pButtons, BorderLayout.SOUTH );
	}
	
	private void selectOK() {
		selectedOption = OK_OPTION;
		setVisible( false );
	}
	
	private void selectCancel() {
		selectedOption = CANCEL_OPTION;
		setVisible( false );
	}
	
	public int getSelectedOption() {
		return selectedOption;
	}
	
	public void addField( String label, JComponent... comp ) {
		JLabel l = new JLabel( label+":" );
		JPanel p1 = new JPanel();
		p1.add( l );
		pContents.add( p1 );
		comps.add( p1 );
		Dimension d = p1.getPreferredSize();
		if( col1Width < d.width ) col1Width = d.width;
		if( maxHeight < d.height ) maxHeight = d.height;
		
		JPanel panel = new JPanel();
		for( JComponent c: comp )
			panel.add( c );

		Dimension size = panel.getPreferredSize();
		if( col2Width<size.width ) col2Width=size.width;
		if( maxHeight<size.height ) maxHeight = size.height;
		
		pContents.add( panel );
		comps.add( panel );
	}
	
	public void pack() {
        Insets insets = pContents.getInsets();
		int h = insets.top;
		for( int i=0;i<comps.size();i+=2 ) {
			Dimension d = comps.get(i).getPreferredSize();
			comps.get(i).setBounds( 0, h, d.width, d.height );
			d = comps.get(i+1).getPreferredSize();
			comps.get(i+1).setBounds( col1Width, h, d.width, d.height );
			h += maxHeight;
		}
		pContents.setPreferredSize( new Dimension( insets.left+col1Width+col2Width+insets.right,
														  h+insets.bottom ) );
		super.pack();
	}
}
